package com.acmen.easyapi.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

/**
 * @author acmen
 */
public class FileLoader {
    private static Map<String, String> jsonNodeMap = new HashMap<String, String>();

    public static String fromResource(String resource) {
        if (jsonNodeMap.get(resource) != null) {
            return jsonNodeMap.get(resource);
        }
        InputStream inputStream = FileLoader.class.getResourceAsStream(resource);
        if (inputStream == null) {
            return null;
        }
        BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
        if (br == null) {
            return null;
        }
        StringBuffer stringBuffer = new StringBuffer();
        String line;
        try {
            while ((line = br.readLine()) != null) {
                stringBuffer.append(line + "\n");
            }
            br.close();
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String content = stringBuffer.toString().trim();
        jsonNodeMap.put(resource, content);
        return content;
    }
}
